package cn.cmdr.service.ServiceImpl;

import cn.cmdr.mapper.SortMapper;
import cn.cmdr.pojo.Brand;
import cn.cmdr.pojo.Commodity;
import cn.cmdr.pojo.Sort;
import cn.cmdr.service.ComService;
import cn.cmdr.service.SortService;
import cn.cmdr.vo.PageVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("sortService")
public class SortServiceImpl extends ServiceImpl<SortMapper, Sort> implements SortService {
    @Resource
    private SortMapper sortMapper;
    @Resource
    private ComService comService;

    @Override
    public PageVo getSortPageList(Integer pageSize, Integer pageNo, Sort sort) {
        Page page = new Page();
        page.setSize(pageSize);
        page.setCurrent(pageNo);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.orderByAsc("sort_row");
        IPage<Sort> iPage = super.page(page,queryWrapper);
        for (Sort s :iPage.getRecords()){
            s.setSortNumber(comService.getsById(s.getId()).size()+"");

        }
        return new PageVo(iPage.getRecords(),iPage.getTotal());
    }

    @Override
    public void updateSort(Sort sort) {
        sortMapper.updateById(sort);
    }

    @Override
    public void deleteSort(Integer id) {
        sortMapper.deleteById(id);
    }

    @Override
    public List<Sort> getSortList(Integer disp) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(disp!=null,"sort_display",disp);
        return sortMapper.selectList(queryWrapper);
    }

    @Override
    public void add(Sort sort) {
        sortMapper.insert(sort);
    }

    @Override
    public Sort getSort(Integer sid) {
        return sortMapper.selectById(sid);
    }

}
